package com.learn.springbootcache.service;

import com.learn.springbootcache.bean.Org;
import com.learn.springbootcache.mapper.OrgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@CacheConfig(cacheNames = {"org"})
@Service
public class OrgService {

    @Autowired
    OrgMapper orgMapper;

    @Cacheable(key = "#p0", unless = "#result == null")
    public Org getOrgById(Integer id){
        return orgMapper.getOrgById(id);
    }

    @CachePut(key = "#result.id")
    public int updateOrg(Org org){
        return orgMapper.updateOrgById(org);
    }

    @CacheEvict(key = "#a0")
    public int delOrg(Integer id){
        return orgMapper.delOrg(id);
    }
}
